import { Collapse, CollapsePanel } from '@components/Collapse';

# ContextModuleFactory 钩子

`Compiler` 使用 `ContextModuleFactory` 模块从 [require.context](/api/runtime-api/module-methods#requirecontext) API 生成依赖关系。它会解析请求的目录，为每个文件生成请求，并依据传递来的 regExp 进行过滤。最后匹配成功的依赖关系将被传入 [NormalModuleFactory](/api/plugin-api/normal-module-factory-hooks)。

## `beforeResolve`

`AsyncSeriesBailHook<[BeforeResolveResult]>`

在解析请求的目录之前调用。通过返回 `false` 可以忽略该请求。

<Collapse>
  <CollapsePanel
    className="collapse-code-panel"
    header="BeforeResolveResult.ts"
    key="BeforeResolveResult"
  >
```ts
type BeforeResolveData = {
  context: string;
  request: string;
  regExp: RegExp | undefined;
  recursive: boolean;
}

export type BeforeResolveResult =
  | false
  | BeforeResolveData;
```
  </CollapsePanel>
</Collapse>

## `afterResolve`

`AsyncSeriesBailHook<[AfterResolveResult]>`

在请求的目录解析后调用。

<Collapse>
  <CollapsePanel
    className="collapse-code-panel"
    header="AfterResolveResult.ts"
    key="AfterResolveResult"
  >
```ts
type AfterResolveData = {
  resource: number;
  context: string;
  request: string;
  regExp: RegExp | undefined;
  recursive: boolean;
  dependencies: Dependency[];
}

export type AfterResolveResult = 
  | false
  | AfterResolveData;
```
  </CollapsePanel>
</Collapse>
